package com.sec.bnjey.svc;

import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.sec.bnjey.bluetooth.BtService;
import com.sec.bnjey.global.Const;
import com.sec.bnjey.utils.RbLog;

public class VtalkBroadcastReceiver extends BroadcastReceiver {
	private static final String TAG = "VtalkBroadcastReceiver";
	
	//alex
	private static final int STATE_CONNECTED = 2;
	private static boolean mbCalledFromVtalk = false;
	//
	
	@Override
	public void onReceive(Context context, Intent intent) {

		Log.w(TAG, "onReceive :" + intent.getFlags());

		if (intent.getAction() != null) {
			if (intent.getAction().equals(
					"android.intent.action.BOOT_COMPLETED")) {
				RbLog.d(TAG, "BOOT_COMPLETED");

				ComponentName cn = new ComponentName(context.getPackageName(),
						DaemonService.class.getName());
				context.startService(new Intent().setComponent(cn));

			} 
			//alex
			else if (intent.getAction().equals(
					"android.bluetooth.device.action.ACL_CONNECTED")) {
				RbLog.d(TAG, "ACL_CONNECTED");
				if (checkBtDevice()) {
					checkBtState();
				}
				getBtState();
			}
			else if (intent.getAction().equals(
					"android.bluetooth.device.action.ACL_DISCONNECTED")) {
				RbLog.d(TAG, "ACL_DISCONNECTED");
				if (getCalledFromVtalk() && checkBtDevice()) {
					setCalledFromVtalk(false);
					ComponentName cn = new ComponentName(context.getPackageName(),
							DaemonService.class.getName());
					context.startService(new Intent().setComponent(cn));				
				}
				getBtState();
			}			
			//
		} else {

			Log.w(TAG, "Received Alarm");

			ComponentName cn = new ComponentName(context.getPackageName(),
					DaemonService.class.getName());
			context.startService(new Intent().setComponent(cn));

		}

	}
	
	//alex
	private boolean getCalledFromVtalk() {
		return mbCalledFromVtalk;
	}
	
	private void setCalledFromVtalk(boolean bCalledFromVtalk) {
		mbCalledFromVtalk = bCalledFromVtalk;
	}
	
	private void checkBtState() {
		if (BtService.mConnectionState == STATE_CONNECTED) {
			setCalledFromVtalk(false);
		} else {
			setCalledFromVtalk(true);
		}
	}
	
	private void getBtState() {
		String state = new String();
		switch (BtService.getBtService().getBluetoothStatus()) {
		case 0: state = "STATE_NONE";	break;
		case 1: state = "STATE_CONNECTING";	break;
		case 2: state = "STATE_CONNECTED";	break;
		case 3: state = "STATE_PAIRING";	break;
		}
		Log.w(TAG, "mConnectionState=" + state + ":Vtalk=" + getCalledFromVtalk());
	}
	
	private boolean checkBtDevice() {
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
			return false;
		}
		
		String aAddr = mBluetoothAdapter.getAddress();
		if (aAddr == null || aAddr.length() == 0) {
			return false;
		}

		Set<BluetoothDevice> pairedDevs = mBluetoothAdapter.getBondedDevices();
		if (pairedDevs.size() > 0) {
			for (BluetoothDevice device : pairedDevs) {
				String pAddress = device.getAddress();
				String pName = device.getName();

				if (pName.equals(Const.OBD_DEVICE_NAME)) {
					Log.w(TAG, "Device " + pName + " addr " + pAddress);
					return true;
				}
			}
		}
		
		return false;
	}
	//
}
